package controllers.transportion;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib_model.Categories.Categories;
import lib_model.Factories.Factory;
import lib_model.Order.Orders_bill_type;
import lib_model.Orther.ReturnObj;
import lib_model.Transportion.Transportion_type;

/**
 * Servlet implementation class AddFactoties
 */
@WebServlet("/transportion/AddTransportionType")
public class AddTransportionType extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddTransportionType() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String name = request.getParameter("txtName");
			String description = request.getParameter("txtDes");
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/transportions-type.jsp");
			if (name != null && description != null
					&& request.getParameter("txtDes") != null) {
				Transportion_type f = new Transportion_type();
				f.name = name;
				if (f.GetInformation().ObjResult != null)
					request.setAttribute("Mes", "This name type has exist !!!");
				else {
					f.description = description;
					ReturnObj obj = f.Add();
					if (obj.Result == true) {
						request.setAttribute("Mes", "Suscessfull !");
					} else {
						request.setAttribute("Mes",
								"Error ! Can't create Transportion-type 2");
					}
				}
			} else {
				request.setAttribute("Mes",
						"Error ! Can't create Transportion-type 3");
			}
			dis.forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/transportions-type.jsp");
			request.setAttribute("Mes",
					"Error ! Can't create Transportion-type");
			dis.forward(request, response);
		}
	}
}
